package com.sinoglobal.ningxia.activity.mico;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.adapter.PlayBillFragmentAdapter;
import com.sinoglobal.ningxia.beans.PlayBillBean;
import com.sinoglobal.ningxia.fragment.PlayBillFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qiwx
 * @createdate 2014-6-23 上午10:57:03
 * @Description: 节目单
 */
public class PlayBillActivity extends BaseActivity implements OnClickListener {
    public static final int TAB_PUBLIC = 0;// 公共
    public static final int TAB_SATELLITE = 1;// 卫视
    public static final int TAB_ECONOMIC = 2;// 经济
    public static final int TAB_MOVIES = 3;// 影视
    public static final int TAB_CHILDRENS = 4;// 少儿
    private ViewPager viewPager;// 5个界面所展示的viewpager
    // 对应的5个不同的模块：公共，卫视，经济，影视，少儿
    public static List<Fragment> lists;
    public static Map<Integer, List<PlayBillBean>> mapData = new HashMap<Integer, List<PlayBillBean>>();
    private PlayBillFragment mPublicChannel; // 公共
    private PlayBillFragment mSatelliteChannel; // 卫视
    private PlayBillFragment mEconomicChanel; // 经济
    private PlayBillFragment mMoviesChanel; // 影视
    private PlayBillFragment mChildrensChanel; // 少儿
    private int publicChannel = 1;
    private int satelliteChannel = 2;
    private int economicChanel = 3;
    private int moviesChanel = 4;
    private int childrensChanel = 5;
    private RadioButton cPublic, satellite, economic, movies, childrens;
    public static Handler myCollectionTabTextNumHandler;// 用于5中界面获取到收藏数之后，显示在Tab上
    private ImageView back;
    // 栏目菜单
    // TextView channelMenu;
    private RelativeLayout Rela_menu1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playbill_activity);
        openChenJInShi();
        titleView.setText("节目单");
        init();
        addListener();
        loadData();
    }

    private void init() {
        cPublic = (RadioButton) findViewById(R.id.public_channel);
        satellite = (RadioButton) findViewById(R.id.satellite_channel);
        economic = (RadioButton) findViewById(R.id.economic_channel);
        movies = (RadioButton) findViewById(R.id.movies_channel);
        childrens = (RadioButton) findViewById(R.id.childrens_channel);
        // channelMenu = (TextView) findViewById(R.id.mic_playbill_menu);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        Rela_menu1 = (RelativeLayout) findViewById(R.id.Rela_menu1);

    }

    // 加载数据
    public void loadData() {
        lists = new ArrayList<Fragment>();
//        ProgramFragment programFragment = ProgramFragment.newInstance(publicChannel);
        mPublicChannel = new PlayBillFragment(publicChannel);
        mSatelliteChannel = new PlayBillFragment(satelliteChannel);
        mEconomicChanel = new PlayBillFragment(economicChanel);
        mMoviesChanel = new PlayBillFragment(moviesChanel);
        mChildrensChanel = new PlayBillFragment(childrensChanel);
//        lists.add(programFragment);
        lists.add(mPublicChannel);
        lists.add(mSatelliteChannel);
        lists.add(mEconomicChanel);
        lists.add(mMoviesChanel);
        lists.add(mChildrensChanel);
        PlayBillFragmentAdapter fragmentAdapter = new PlayBillFragmentAdapter(getSupportFragmentManager());
        fragmentAdapter.setLists(lists);
        viewPager.setAdapter(fragmentAdapter);
    }

    @Override
    protected void onDestroy() {
        // myTask Auto-generated method stub
        super.onDestroy();
        if (mapData != null)
            mapData.clear();
        if (lists != null)
            lists.clear();

    }

    private void addListener() {
        cPublic.setOnClickListener(this);
        satellite.setOnClickListener(this);
        economic.setOnClickListener(this);
        movies.setOnClickListener(this);
        childrens.setOnClickListener(this);
        Rela_menu1.setOnClickListener(this);
        // channelMenu.setOnClickListener(this);

        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int id) {
                switch (id) {
                    // 公共频道
                    case TAB_PUBLIC:
                        cPublic.setChecked(true);
                        // mPublicChannel.getData();
                        break;
                    // 卫视频道
                    case TAB_SATELLITE:
                        satellite.setChecked(true);
                        // mSatelliteChannel.getData();
                        break;
                    // 经济频道
                    case TAB_ECONOMIC:
                        economic.setChecked(true);
                        // mEconomicChanel.getData();
                        break;
                    // 影视频道
                    case TAB_MOVIES:
                        movies.setChecked(true);
                        // mMoviesChanel.getData();
                        break;
                    // 少儿频道
                    case TAB_CHILDRENS:
                        childrens.setChecked(true);
                        // mChildrensChanel.getData();
                        break;
                }

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 栏目menu
            case R.id.Rela_menu1:
                finish();
                break;
            // 公共
            case R.id.public_channel:
                viewPager.setCurrentItem(TAB_PUBLIC);
                break;
            // 卫视
            case R.id.satellite_channel:
                viewPager.setCurrentItem(TAB_SATELLITE);
                break;
            // 经济
            case R.id.economic_channel:
                viewPager.setCurrentItem(TAB_ECONOMIC);
                break;
            // 影视
            case R.id.movies_channel:
                viewPager.setCurrentItem(TAB_MOVIES);
                break;
            // 少儿
            case R.id.childrens_channel:
                viewPager.setCurrentItem(TAB_CHILDRENS);
                break;

        }

    }
}
